#include "barwidget.h"

BarWidget::BarWidget()
{
    setGeometry(QRect(0,0,window()->width()* 0.3, 30));
    setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    createWidgets();
}

// This automatically closes the window.
void BarWidget::closeEvent(QCloseEvent *event)
{
    event->accept();
}

// Create widgets.
void BarWidget::createWidgets()
{
    // TODO.
    hLayout = new QHBoxLayout();
    hLayout->setContentsMargins(20,0,20,0);

    buttonIE = new QPushButton(QIcon(":/images/IE.png"),"",this);
    buttonChrome = new QPushButton(QIcon(":/images/Chrome.png"),"",this);
    buttonMozilla = new QPushButton(QIcon(":/images/Firefox.png"),"",this);
    buttonSafari = new QPushButton(QIcon(":/images/Safari.png"),"",this);
    buttonSettings = new QPushButton(QIcon(":/images/gear.png"),"",this);

    connect(buttonIE,SIGNAL(clicked()),this,SLOT(goIESlot()));
    connect(buttonChrome,SIGNAL(clicked()),this,SLOT(goChromeSlot()));
    connect(buttonMozilla,SIGNAL(clicked()),this,SLOT(goMozillaSlot()));
    connect(buttonSafari,SIGNAL(clicked()),this,SLOT(goSafariSlot()));
    connect(buttonSettings,SIGNAL(clicked()),this, SLOT(settings()));

    searchText = new QLineEdit();
    completer = new QCompleter();
    searchText->setCompleter(completer);
    searchText->setFixedWidth(200);
    searchText->setMinimumHeight(32);
    searchText->setTextMargins(3,3,3,3);

    connect(searchText,SIGNAL(returnPressed()),this,SLOT(search()));

    hLayout->addWidget(buttonIE,0,Qt::AlignLeft);
    hLayout->addWidget(buttonChrome,0,Qt::AlignLeft);
    hLayout->addWidget(buttonMozilla,0,Qt::AlignLeft);
    hLayout->addWidget(buttonSafari,0,Qt::AlignLeft);
    hLayout->addWidget(buttonSettings,0,Qt::AlignLeft);
    hLayout->addWidget(searchText,0,Qt::AlignRight);
    hLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Fixed));
    setLayout(hLayout);
}


// Slot functions

// This slot function goes to IE.
void BarWidget::goIESlot()
{
    Thumbnail* selected = GridWidget::getInstance()->getSelected();
    if(selected == 0) return;
    QProcess* p = new QProcess();
    p->start(settingsDialog::getInstance()->getIEPath(),QStringList()<<selected->getData().address);
}
// This slot function goes to Mozille Firefox.
void BarWidget::goMozillaSlot()
{
    Thumbnail* selected = GridWidget::getInstance()->getSelected();
    if(selected == 0) return;
    QProcess* p = new QProcess();
    p->start(settingsDialog::getInstance()->getFireFoxPath(),QStringList()<<selected->getData().address);
}
// This slot function goes to Chrome.
void BarWidget::goChromeSlot()
{
    Thumbnail* selected = GridWidget::getInstance()->getSelected();
    if(selected == 0) return;
    QProcess* p = new QProcess();
    p->start(settingsDialog::getInstance()->getChromePath(),QStringList()<<selected->getData().address);
}
// This slot function goes to Safari.
void BarWidget::goSafariSlot()
{
    Thumbnail* selected = GridWidget::getInstance()->getSelected();
    if(selected == 0) return;
    QProcess* p = new QProcess();
    p->start(settingsDialog::getInstance()->getOperaPath(),QStringList()<<selected->getData().address);
}

void BarWidget::search()
{
    QString text = searchText->text();
    QList<QTreeWidgetItem*> list = ActualDataModel::inOrderTraversal(text,NAME,ActualDataModel::contains);
    //qDebug()<<list.at(0)->text(0);
    GridWidget::getInstance()->displayList(list);
}

void BarWidget::settings()
{
    settingsDialog::getInstance()->show();
}
